package com.tianhua.javs.client.bean;


import com.google.common.collect.Lists;
import com.tianhua.javs.client.model.FieldModel;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * date: 2022/2/10
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Data
public class JavsSegmentBean {

    /**
     * 转换后的代码段
     */
    private String segment;

    /**
     * 当前转换代码的内容需要导入的包
     */
    private List<String> packageList = new ArrayList<>();


    /**
     * 代码段对应key在转换java原生代码的时候需要加入的后缀，用来补充
     * 括号,满足Java原生代码的语法
     * @return
     */
    private String keySuffix = "";

    /**
     * 代码段对应value在转换Java原生代码的时候需要加入的后缀，用来补充括号，满足Java原生代码的语法
     * @return
     */
    private String valueSuffix = "";


    /**
     * 转换后的代码可能代表的变量类型
     * 主要赋值语句可能需要
     */
    private String segmentClass;

    /**
     * 在进行解析过程中进行打标判断是否匹配了某次函数解析替换
     */
    private boolean match;


    /**
     * 在脚本代码段解析运行时中新构建的变量信息
     *
     */
    List<ScriptVarMappingBean> scriptVarMappingBeanList;


    /**
     * 在脚本代码段解析运行时中新构建的变量信息
     * 单条
     */
    private ScriptVarMappingBean scriptVarMappingBean;

    /**
     * 解析全局变量作为类的属性
     * 此时JavsSegmentBean是一个容器数据在fieldModel中
     */
    private FieldModel fieldModel;


    public String getSegmentClass() {
        return segmentClass;
    }

    public void setSegmentClass(String segmentClass) {
        this.segmentClass = segmentClass;
    }

    public boolean isMatch() {
        return match;
    }

    public void setMatch(boolean match) {
        this.match = match;
    }

    public String getSegment() {
        return segment;
    }

    public void setSegment(String segment) {
        this.segment = segment;
    }

    public List<String> getPackageList() {
        return packageList;
    }

    public void setPackageList(List<String> packageList) {
        this.packageList = packageList;
    }

    public String getKeySuffix() {
        return keySuffix;
    }

    public void setKeySuffix(String keySuffix) {
        this.keySuffix = keySuffix;
    }

    public String getValueSuffix() {
        return valueSuffix;
    }

    public void setValueSuffix(String valueSuffix) {
        this.valueSuffix = valueSuffix;
    }

    public JavsSegmentBean(String segment){
        this.segment = segment;
    }

    public static JavsSegmentBean getInstance(String segment){
        return new JavsSegmentBean(segment);
    }

    public static JavsSegmentBean getMatchInstance(String segment){
        JavsSegmentBean javsSegmentBean = JavsSegmentBean.getInstance(segment);
        javsSegmentBean.setMatch(true);
        return javsSegmentBean;
    }

    public static JavsSegmentBean getMatchInstance(FieldModel fieldModel){
        JavsSegmentBean javsSegmentBean = JavsSegmentBean.getInstance("");
        javsSegmentBean.setMatch(true);
        javsSegmentBean.setFieldModel(fieldModel);
        return javsSegmentBean;
    }


    public static JavsSegmentBean getMatchInstance(String segment, String segmentClass){
        JavsSegmentBean javsSegmentBean = JavsSegmentBean.getInstance(segment);
        javsSegmentBean.setMatch(true);
        javsSegmentBean.setSegmentClass(segmentClass);
        return javsSegmentBean;
    }

    public static JavsSegmentBean getMatchInstance(String segment, String segmentClass,String ...importClass){
        JavsSegmentBean javsSegmentBean = JavsSegmentBean.getInstance(segment);
        javsSegmentBean.setMatch(true);
        javsSegmentBean.setSegmentClass(segmentClass);
        javsSegmentBean.setPackageList(Lists.newArrayList(importClass));
        return javsSegmentBean;
    }

    public static JavsSegmentBean getInstance(String segment, String keySuffix, String valueSuffix){
        JavsSegmentBean javsSegmentBean = new JavsSegmentBean(segment);
        javsSegmentBean.setKeySuffix(keySuffix);
        javsSegmentBean.setValueSuffix(valueSuffix);
        return javsSegmentBean;
    }




    public static JavsSegmentBean getInstance(String segment, String keySuffix, String valueSuffix,String [] packageArr){
        JavsSegmentBean javsSegmentBean = JavsSegmentBean.getInstance(segment, keySuffix, valueSuffix);
        for (String packageName : packageArr){
            if(StringUtils.isEmpty(packageName)){
                continue;
            }
            javsSegmentBean.getPackageList().add(packageName);
        }
        return javsSegmentBean;
    }


    public boolean match(){ return  this.match == true;}


    public void addPackageName(String packageName){
        if(this.packageList.contains(packageName)){
            return;
        }
        this.packageList.add(packageName);
    }
}
